package com.jhz.speedy.common.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 可以手动调用刷新的SwipeRefreshLayout
 * Created by JunHeZ on 2016/3/19.
 */
public class MySwipeRefreshLayout extends SwipeRefreshLayout {
    public MySwipeRefreshLayout(Context context) {
        super(context.getApplicationContext(), null);
        init();
    }

    public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context.getApplicationContext(), attrs);
        init();
    }

    private void init(){
        this.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);

    }

    /**
     * 手动调用刷新
     */
    public void autoRefresh() {
        if(isRefreshing()){
            this.setRefreshing(false);
        }

        try {
            Field mCircleView = SwipeRefreshLayout.class.getDeclaredField("mCircleView");
            mCircleView.setAccessible(true);
            View progress = (View) mCircleView.get(this);
            progress.setVisibility(VISIBLE);

            Method setRefreshing = SwipeRefreshLayout.class.getDeclaredMethod("setRefreshing", boolean.class, boolean.class);
            setRefreshing.setAccessible(true);
            setRefreshing.invoke(this, true, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
